package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.CPLibBean;
import com.taptech.doufu.bean.ResponseCPLibBean;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.fragment.CpLibListFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;


/**
 * cp库
 */
public class CpLibListActivity extends DiaobaoBaseActivity implements View.OnClickListener {

    public static final String CPINDEXDATA = "CPINDEXDATA";
    public static final String CPLIBDATA = "CPLIBDATA";

    private TextView mCpLibSortA;
    private TextView mCpLibSortB;
    private TextView mCpLibSortC;
    private TextView mCpLibSortD;


    private ViewPager mCpLibViewPager;
    private static final String TAG = CpLibListActivity.class.getSimpleName();


    private String[] sideBarIndex;
    private String[] sideBarIndexA;
    private String[] sideBarIndexB;
    private String[] sideBarIndexC;
    private String[] sideBarIndexD;

    private ArrayList<CPLibBean> cplibs = new ArrayList<>();
    private ArrayList<CPLibBean> cplibsA = new ArrayList<>();
    private ArrayList<CPLibBean> cplibsB = new ArrayList<>();
    private ArrayList<CPLibBean> cplibsC = new ArrayList<>();
    private ArrayList<CPLibBean> cplibsD = new ArrayList<>();


    private ImageView mSearchImg;

    private int selectTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.text_color_42);
    private int nomalTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.text_color_57);
    private int nomalTextBg = WeMediaApplication.getInstance().getResources().getColor(R.color.white);

    private int selectDarkTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.cp_top_head_dark_color);
    private int nomalDarkTextColor = WeMediaApplication.getInstance().getResources().getColor(R.color.theme_main_dark);
    private int nomalDarkTextBg = WeMediaApplication.getInstance().getResources().getColor(R.color.fg_dark);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cp_lib_layout);

        initView();
        initEvent();
        initData();
    }

    private void initData() {
        ApiClient.getInstance().getService().getCPLibListResponse()
                .compose(RxJavaHelper.<ResponseCPLibBean>observeOnMainThread(this))
                .subscribe(new BaseSubscriber<ResponseCPLibBean>(CpLibListActivity.this) {
                       @Override
                       public void onNext(ResponseCPLibBean responseCPLibBean) {
                           super.onNext(responseCPLibBean);
                           List<ResponseCPLibBean.DataBean.ListBean> cpLibsAll = responseCPLibBean.getData().getList();
                           for (int i = 0; i < cpLibsAll.size(); i++) {
                               setCpLibData(i + 1, cpLibsAll.get(i));
                           }
                           setTab(1);
                           mCpLibViewPager = (ViewPager) findViewById(R.id.cp_lib_view_pager);
                           mCpLibViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                               @Override
                               public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                               }

                               @Override
                               public void onPageSelected(int position) {
                                   setTab(position + 1);
                               }

                               @Override
                               public void onPageScrollStateChanged(int state) {

                               }
                           });
                           mCpLibViewPager.setAdapter(new CPLibViewPagerAdapter(getSupportFragmentManager()));
                       }
                   }
                );
    }


    private void setCpLibData(int position, ResponseCPLibBean.DataBean.ListBean cpLibsData) {
        if (sideBarIndex != null && sideBarIndex.length != 0) {
            sideBarIndex = null;
        }
        if (cplibs != null && cplibs.size() > 0) {
            cplibs = null;
            cplibs = new ArrayList<>();
        }

        String tabText = cpLibsData.getTitle();
        List<ResponseCPLibBean.DataBean.ListBean.ListBeanCPList> cpLibs = cpLibsData.getList();
        sideBarIndex = new String[cpLibs.size()];
        for (int i = 0; i < cpLibs.size(); i++) {
            sideBarIndex[i] = cpLibs.get(i).getIndex_char();
            addCpLibByIndex(sideBarIndex[i], cpLibs.get(i).getList());
        }
        switch (position) {
            case 1:
                mCpLibSortA.setText(tabText);
                sideBarIndexA = sideBarIndex;
                cplibsA = cplibs;
                break;
            case 2:
                mCpLibSortB.setText(tabText);
                sideBarIndexB = sideBarIndex;
                cplibsB = cplibs;
                break;
            case 3:
                mCpLibSortC.setText(tabText);
                sideBarIndexC = sideBarIndex;
                cplibsC = cplibs;
                break;
            case 4:
                mCpLibSortD.setText(tabText);
                sideBarIndexD = sideBarIndex;
                cplibsD = cplibs;
                break;
        }
    }

    private void addCpLibByIndex(String index, List<ResponseCPLibBean.DataBean.ListBean.ListBeanCPList.ListBeanCP> list) {
        for (int i = 0; i < list.size(); i++) {
            CPLibBean cp = new CPLibBean() {
                @Override
                public int describeContents() {
                    return 0;
                }

                @Override
                public void writeToParcel(Parcel dest, int flags) {
                }
            };
            cp.setIndex(index);
            cp.setItem_cp_lib_name(list.get(i).getName());
            cp.setItem_cp_lib_img(list.get(i).getIcon());
            cp.setItem_cp_lib_count(list.get(i).getCount());
            cp.setItem_cp_lib_flower_count(list.get(i).getFlower_count());
            cp.setId(String.valueOf(list.get(i).getId()));
            cplibs.add(cp);
        }
    }


    private void initView() {
        mCpLibSortA = (TextView) findViewById(R.id.cp_lib_sort_a);
        mCpLibSortB = (TextView) findViewById(R.id.cp_lib_sort_b);
        mCpLibSortC = (TextView) findViewById(R.id.cp_lib_sort_c);
        mCpLibSortD = (TextView) findViewById(R.id.cp_lib_sort_d);
        mSearchImg = (ImageView) findViewById(R.id.cp_lib_search_cp);
    }

    private void initEvent() {
        mCpLibSortA.setOnClickListener(this);
        mCpLibSortB.setOnClickListener(this);
        mCpLibSortC.setOnClickListener(this);
        mCpLibSortD.setOnClickListener(this);
        mSearchImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mCpLibViewPager == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.cp_lib_sort_a:
                setTab(1);
                mCpLibViewPager.setCurrentItem(0);
                break;
            case R.id.cp_lib_sort_b://日本
                setTab(2);
                mCpLibViewPager.setCurrentItem(1);
                break;
            case R.id.cp_lib_sort_c://欧美
                setTab(3);
                mCpLibViewPager.setCurrentItem(2);
                break;
            case R.id.cp_lib_sort_d://真人
                mCpLibViewPager.setCurrentItem(3);
                setTab(4);
                break;
            case R.id.cp_lib_search_cp:
                Intent intent = new Intent(CpLibListActivity.this, CpSearchActivity.class);
                startActivity(intent);
                break;
        }


    }

    /**
     * set tab style
     *
     * @param position
     */
    private void setTab(int position) {
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            nomalTextColor = nomalDarkTextColor;
            nomalTextBg = nomalDarkTextBg;
            selectTextColor = selectDarkTextColor;
        }
        mCpLibSortA.setTextColor(nomalTextColor);
        mCpLibSortB.setTextColor(nomalTextColor);
        mCpLibSortC.setTextColor(nomalTextColor);
        mCpLibSortD.setTextColor(nomalTextColor);
        mCpLibSortA.setBackgroundColor(nomalTextBg);
        mCpLibSortB.setBackgroundColor(nomalTextBg);
        mCpLibSortC.setBackgroundColor(nomalTextBg);
        mCpLibSortD.setBackgroundColor(nomalTextBg);
        switch (position) {
            case 1:
                mCpLibSortA.setTextColor(selectTextColor);
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    mCpLibSortA.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_left_night);
                } else {
                    mCpLibSortA.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_left);
                }
                break;
            case 2:
                mCpLibSortB.setTextColor(selectTextColor);
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    mCpLibSortB.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_night);
                } else {
                    mCpLibSortB.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg);
                }

                break;
            case 3:
                mCpLibSortC.setTextColor(selectTextColor);
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    mCpLibSortC.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_night);
                } else {
                    mCpLibSortC.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg);
                }
                break;
            case 4:
                mCpLibSortD.setTextColor(selectTextColor);
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    mCpLibSortD.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_right_night);
                } else {
                    mCpLibSortD.setBackgroundResource(R.drawable.cp_gay_sort_text_selected_bg_right);
                }
                break;
        }

    }


    public class CPLibViewPagerAdapter extends FragmentPagerAdapter {

        public CPLibViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();
            switch (position) {
                case 0:
                    bundle.putStringArray(CPINDEXDATA, sideBarIndexA);
                    bundle.putParcelableArrayList(CPLIBDATA, cplibsA);
                    break;
                case 1:
                    bundle.putStringArray(CPINDEXDATA, sideBarIndexB);
                    bundle.putParcelableArrayList(CPLIBDATA, cplibsB);
                    break;
                case 2:
                    bundle.putStringArray(CPINDEXDATA, sideBarIndexC);
                    bundle.putParcelableArrayList(CPLIBDATA, cplibsC);
                    break;
                case 3:
                    bundle.putStringArray(CPINDEXDATA, sideBarIndexD);
                    bundle.putParcelableArrayList(CPLIBDATA, cplibsD);
                    break;
            }
            CpLibListFragment cpLibListFragment = new CpLibListFragment();
            cpLibListFragment.setArguments(bundle);
            return cpLibListFragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }

}



